package pers.cz.chat.filter;

import io.netty.channel.Channel;
import io.netty.util.internal.StringUtil;
import pers.cz.chat.constants.JefClolorEnum;
import pers.cz.chat.utils.LogUtils;
import pers.cz.netty.filter.FilterHandler;
import pers.cz.netty.filter.FilterHandlerContext;
import pers.cz.netty.protocal.Message;
import pers.cz.netty.protocal.MsgType;
import pers.cz.netty.util.ContextHolder;

/**
 * @version 1.0
 * @description: 接收登录消息拦截器
 * @author: ChengZhi 0336
 * @create: 2025-01-06 18:11
 **/
public class LoginReceiveFilter implements FilterHandler<Message, Channel> {

	@Override
	public boolean approve(Message message, Channel obj, FilterHandlerContext<Message,Channel> handlerContext) {
		if (MsgType.AUTH.equals(message.getMsgType())) {
			LogUtils.print(JefClolorEnum.GREEN, message.getContent().toString());
			// 判断是否登录成功
			String result = message.getAttachment("result");
			if (!StringUtil.isNullOrEmpty(result) && "success".equals(result)) {
				ContextHolder.getRequestContext().put("sourceUserName", message.getAttachment("target"));
				ContextHolder.getRequestContext().put("loginType", "1");
			}
		}

		return handlerContext.doFilter(message, obj);
	}
}
